package com.example.Sao.MyFragment;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.Sao.Adapter.ProgressAdpter;
import com.example.Sao.MyBean.Lbean;
import com.example.Sao.ObjectBox.DeviceEntity;
import com.example.Sao.ObjectBox.DeviceEntity_;
import com.example.Sao.R;
import com.example.Sao.Tools.ObjectBoxHelper;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import io.reactivex.functions.Consumer;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.MyFragment
 * @ClassName: CensusFragment
 * @Description: 统计用Fragment
 * @Author: 阿毛
 * @CreateDate: 2020/2/21 17:05
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/2/21 17:05
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class CensusFragment extends LazyloadFragment {
    private LineChart mLineChart;
    private RecyclerView recyclerView;
    private ObjectBoxHelper objectBoxHelper;
    @Override
    protected void initView(View view) {
        mLineChart = view.findViewById(R.id.mLineChart);
        recyclerView  =view.findViewById(R.id.RecyclerView);
        objectBoxHelper = ObjectBoxHelper.getInstance();
        super.initView(view);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_census;
    }

    @Override
    protected boolean loadData() {
            drawline();
            List<Lbean>lbeanList = new ArrayList<>();
//            float pro1 = 0;
//             float pro2 = 0;
//              float pro3 = 0;
//           List<DeviceEntity>entities1 =  objectBoxHelper.Query("学习部",DeviceEntity_.useDepartment);
//           List<DeviceEntity>entities2 =  objectBoxHelper.Query("体育部",DeviceEntity_.useDepartment);
//           List<DeviceEntity>entities3 =  objectBoxHelper.Query("生活部",DeviceEntity_.useDepartment);
//           for (DeviceEntity entity:entities1){
//               if (entity.isStaus()){
//                   pro1++;
//               }
//           }
//        for (DeviceEntity entity:entities2){
//            if (entity.isStaus()){
//                pro2++;
//            }
//        }
//        for (DeviceEntity entity:entities3){
//            if (entity.isStaus()){
//                pro3++;
//            }
//        }
        Lbean lbean1  =new Lbean("学习部",20);
        Lbean lbean2  =new Lbean("体育部",25);
        Lbean lbean3  =new Lbean("生活部",40);
        lbeanList.add(lbean1);
        lbeanList.add(lbean2);
        lbeanList.add(lbean3);
        ProgressAdpter progressAdpter = new ProgressAdpter(getContext(),lbeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(progressAdpter);
        return true;
    }

    public void drawline(){
        //是否展示网格线
        mLineChart.setDrawGridBackground(false);
        //是否显示边界
        mLineChart.setDrawBorders(false);
        mLineChart.setBorderColor(Color.GRAY);
        mLineChart.setDrawGridBackground(false);
        mLineChart.animateXY(1000, 1000);

        Legend legend = mLineChart.getLegend();
        legend.setEnabled(false);
        //设置Y轴样式
        YAxis leftYAxis = mLineChart.getAxisLeft();
        leftYAxis.setDrawGridLines(false);
        leftYAxis.setTextSize(16f);
        leftYAxis.setTextColor(Color.parseColor("#acb7c8"));
        //设置图表左边的y轴禁用
        leftYAxis.setEnabled(true);

        YAxis rightYAxis = mLineChart.getAxisRight();
        rightYAxis.setDrawGridLines(false);
        rightYAxis.setAxisMinimum(0f);
        rightYAxis.setTextSize(16f);
        rightYAxis.setTextColor(Color.parseColor("#acb7c8"));
        //设置图表右边的y轴禁用
        rightYAxis.setEnabled(false);

        //设置x轴
        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setTextColor(Color.parseColor("#acb7c8"));
        xAxis.setTextSize(16f);
        xAxis.setDrawAxisLine(true);//是否绘制轴线
        xAxis.setDrawGridLines(false);//设置x轴上每个点对应的线
        xAxis.setDrawLabels(true);//绘制标签  指x轴上的对应数值
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴的显示位置
        xAxis.setGranularity(1f);//禁止放大后x轴标签重绘

        Description description = new Description();
        description.setEnabled(false);
        mLineChart.setDescription(description);
        //显示边界
        mLineChart.setDrawBorders(true);
        //设置数据
        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            entries.add(new Entry(i, (float) new Random().nextInt(99)%(99-10+1) + 10));
        } //一个LineDataSet就是一条线
        LineDataSet lineDataSet = new LineDataSet(entries, "");
        lineDataSet.setColor(Color.parseColor("#e7498af9"));
        lineDataSet.setCircleColor(Color.parseColor("#e7498af9"));
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFillDrawable(getResources().getDrawable(R.drawable.line_gradient_bule_shape));
        LineData data = new LineData(lineDataSet);
        data.setDrawValues(false);
        mLineChart.setData(data);
    }
}
